package com.example.cc.client.autoconfigure;

import com.example.cc.client.service.ReigsterSyncService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.lang.NonNull;
import org.springframework.stereotype.Component;

@Component
public class RegisterSelfListener implements ApplicationListener<ContextRefreshedEvent> {

    private final ReigsterSyncService configSyncService;

    @Autowired
    public RegisterSelfListener(ReigsterSyncService configSyncService) {
        this.configSyncService = configSyncService;
    }

    /**
     * 当ApplicationContext初始化或刷新完成时，自动注册自己到配置中心
     * @param event 事件对象
     */
    @Override
    public void onApplicationEvent(@NonNull ContextRefreshedEvent event) {
        configSyncService.syncAndRegister();
    }
}
